<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>注册</title>
  <link rel="stylesheet" href="../static/css/register.css">
  <link rel="stylesheet" href="../static/css/bootstrap.min.css">
</head>

</head>


<body>
  <div class="row align-items-center">
    <div class="col"></div>
    <div class="col">
      <div class="alert alert-dark invisible" role="alert">
        <strong>提示!</strong>
        <font id="msgId">错误！</font>
      </div>
    </div>

    <div class="col"></div>

  </div>
  <div class="htmleaf-container">
    <form action="" id="registerForm">
      <div class="wrapper">

        <body translate="no">
          <div class="login-box">
            <h2>R E G I S T E R</h2>
            <form class="form">
              <div class="user-box">
                <input type="text" name="username" id="username" required="">
                <label>Username</label>
              </div>
              <div class="user-box">
                <input type="password" name="password" id="password" required="">
                <label>Password</label>
              </div>
              
              <div class="user-box">
                <input type="password" name="repassword" id="repassword" required="">
                <label>确认密码</label>
              </div>
              <a href="#" onclick="jump()" style="font-family: 楷体;">
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                注册
              </a>
              <a href="#" onclick="window.location.href='login.html'"style="font-family: 楷体;">
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                取消
              </a>
            </form>
          </div>
      </div>
    </form>
  </div>


  <script src="../static/js/jquery-3.6.0.min.js"></script>
  <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
  <script src="../static/js/bootstrap.min.js"></script>
  <script>
    $('#login-button').click(function (event) {
      event.preventDefault();
      $('form').fadeOut(500);
      $('.wrapper').addClass('form-success');
    });
  </script>
  <script>
    function jump() {
      let uid = $('#username').val();
      let pwd = $('#password').val();
      let confirmpwd = $('#repassword').val();

      if (uid.length > 0 && pwd.length > 0 && pwd === confirmpwd) {
        axios({
          method: 'post',
          url: '/registerDone',
          data: $('#registerForm').serialize(),
        }).then(function (res) {
          res = res.data;
          if (res.code === 200) {
            window.location.href = '/views/login.html';
          } else {
            alert_fn(res.msg);
            console.log(res.msg);
          }
        }).catch((err) => {
          alert_fn(err);
          console.log(err);
        });
      } else {
        alert_fn('用户名、密码不能为空，并且两次密码应该一致');
      }
    }

    function alert_fn(msg) {
      $('#msgId').text(msg);
      $('.alert').removeClass('invisible');
      setTimeout(() => {
        $('.alert').addClass('invisible');
      }, 3000);
    }
  </script>
</body>

</html>